﻿#region Copyright (c) 2008 plexBB Team

/*  Copyright (c) 2008 plexBB Team
 *  
 *  This file is free software: you may copy, redistribute and/or modify it  
 *  under the terms of the GNU General Public License as published by the  
 *  Free Software Foundation, either version 2 of the License, or (at your  
 *  option) any later version.  
 *  
 *  This file is distributed in the hope that it will be useful, but  
 *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 *  General Public License for more details.  
 *  
 *  You should have received a copy of the GNU General Public License  
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

#endregion

using System;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using PlexBB.Data;

namespace PlexBB.Controls.Admin.ForumManagement
{
    public abstract class AcpForumEdit : AcpForumControlBase
    {
        private const string _dummyPassword = "      ";
        private Forum _editForum;
        private bool _isNew;

        #region Controls

        protected IButtonControl bForumSubmit;
        protected Control cCatToLinkActions;
        protected Control cForumImage;
        protected Control cForumTitle;
        protected Control cTypeActions;
        protected HyperLink hBack;
        protected Image iForumImage;
        protected ICheckBoxControl kDeletePosts;
        protected ICheckBoxControl kDeleteSubforums;
        protected ICheckBoxControl kDescriptionParseBBCode;
        protected ICheckBoxControl kDescriptionParseSmilies;
        protected ICheckBoxControl kDescriptionParseUrls;
        protected ICheckBoxControl kForumPasswordUnset;
        protected ICheckBoxControl kMovePosts;
        protected ICheckBoxControl kMoveSubforums;
        protected ICheckBoxControl kRulesParseBBCode;
        protected ICheckBoxControl kRulesParseSmilies;
        protected ICheckBoxControl kRulesParseUrl;
        protected ListControl sCopyPersmissions;
        protected ListControl sDisplayActiveTopics;
        protected ListControl sDisplayOnIndex;
        protected ListControl sDisplayRecent;
        protected ListControl sDisplaySubforumsList;
        protected ListControl sEnableIcons;
        protected ListControl sEnableIndexing;
        protected ListControl sEnablePostReview;
        protected ListControl sEnablePrune;
        protected ListControl sForumLinkTrack;
        protected ListControl sForumParentID;
        protected ListControl sForumStatus;
        protected ListControl sForumStyle;
        protected ListControl sForumType;
        protected ListControl sLinkDisplayOnIndex;
        protected ListControl sMovePostsTo;
        protected ListControl sMoveSubforumsTo;
        protected ListControl sPruneAnnounce;
        protected ListControl sPruneOldPolls;
        protected ListControl sPruneSticky;
        protected ITextControl tCopyPermissionsExplain;
        protected ITextControl tForumDescription;
        protected ITextControl tForumImage;
        protected ITextControl tForumLink;
        protected ITextControl tForumName;
        protected ITextControl tForumPassword;
        protected ITextControl tForumPasswordConfirm;
        protected ITextControl tForumRules;
        protected ITextControl tForumRulesLink;
        protected ITextControl tForumTitle;
        protected ITextControl tPruneDays;
        protected ITextControl tPruneFrequency;
        protected ITextControl tPruneViewed;
        protected ITextControl tTitle;
        protected ITextControl tTopicsPerPage;

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // setup global variables
            int editForumID = int.Parse(Request.QueryString["eid"]);
            _isNew = editForumID == 0;
            if (_isNew)
            {
                _editForum = new Forum
                                 {
                                     ForumType = ForumType.Forum,
                                     Name = Request.QueryString["n"]
                                 };
            }
            else
            {
                _editForum = Data.Forums.Single(f => f.ForumID == editForumID);
            }

            if (!IsPostBack)
            {
                // TODO: Setup the rest of controls
                sForumType.Text = _editForum.ForumType.ToString();

                ListItem noParentItem = new ListItem(Lang["NO_PARENT"], string.Empty);
                sForumParentID.Items.Add(noParentItem);
                AddForumsToListRecursive(null, sForumParentID, 0);

                tForumName.Text = _editForum.Name;
                tForumDescription.Text = _editForum.Description;
                iForumImage.ImageUrl = tForumImage.Text = _editForum.ImageUrl;
                cForumImage.Visible = _editForum.ImageUrl.Length > 0;

                ((WebControl)tForumPassword).Attributes["Value"] =
                    ((WebControl)tForumPasswordConfirm).Attributes["Value"] = _editForum.HasPassword ? _dummyPassword : string.Empty;

                if (_isNew)
                {
                    cTypeActions.Visible = cCatToLinkActions.Visible = false;
                    tTitle.Text = Lang["CREATE_FORUM"];
                    tForumTitle.Text = _editForum.Name;
                    cForumTitle.Visible = tForumTitle.Text.Length > 0;
                }
            }

            bForumSubmit.Click += bForumSubmit_Click;
        }

        private void bForumSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (_isNew)
                {
                    Data.Forums.InsertOnSubmit(_editForum);
                }

                _editForum.Name = tForumName.Text;
                _editForum.ForumType = (ForumType)Enum.Parse(typeof(ForumType), sForumType.Text);

                Data.SubmitChanges();
            }
        }


        private void AddForumsToListRecursive(int? parentForumID, ListControl control, int level)
        {
            var categories = from f in Data.Forums
                             where Equals(f.ParentID, parentForumID)
                             orderby f.Position
                             select new { f.ForumID, f.Name };
            foreach (var cat in categories)
            {
                StringBuilder catName = new StringBuilder();
                const char nonBreakSpace = (char)0xA0;
                for (int i = 0; i < level * 3; i++)
                {
                    catName.Append(nonBreakSpace);
                }
                catName.Append(cat.Name);

                ListItem item = new ListItem(catName.ToString(), cat.ForumID.ToString());
                control.Items.Add(item);
                AddForumsToListRecursive(cat.ForumID, control, level + 1);
            }
        }
    }
}